/**
 * 特定金额筛查分析器 - 对象中心化版本
 * 检测整数金额或特殊金额的交易（如5万、10万等）
 */

class SpecialAmountAnalyzer extends BaseAnalyzer {
  constructor(caseId, targetSubject = null) {
    super(caseId, targetSubject);
    this.name = '特定金额筛查';
    this.description = '检测整数金额或特殊金额的交易';
  }

  async analyze(params = {}) {
    const targetAmounts = params.targetAmounts || [50000, 100000, 200000, 500000, 1000000];
    const tolerance = params.tolerance || 100; // 容差

    const accountFilter = this.getBankAccountFilter();

    // 生成金额条件
    const amountConditions = targetAmounts.map(amt =>
      `(ABS(transaction_amount) BETWEEN ${amt - tolerance} AND ${amt + tolerance})`
    ).join(' OR ');

    const sql = `
            SELECT 
                account_card,
                customer_name,
                transaction_time,
                transaction_amount,
                debit_credit_flag,
                counterparty_name
            FROM bank_transactions
            WHERE case_id = ?
                AND (${amountConditions})
                ${accountFilter}
            ORDER BY ABS(transaction_amount) DESC
            LIMIT 100
        `;

    const result = await this.runQuery(sql, [this.caseId]);

    if (!result.success) {
      return this.createResult('special_amount', this.name, [], '查询失败: ' + result.message, 'danger');
    }

    const data = result.data;
    const summary = data.length > 0
      ? `检测到 ${data.length} 笔特定金额交易`
      : '未检测到特定金额交易';

    const level = data.length > 20 ? 'warning' : (data.length > 0 ? 'info' : 'info');

    return this.createResult('special_amount', this.name, data, summary, level);
  }

  renderResult(data) {
    if (!data || data.length === 0) {
      return '<div class="empty-result" style="padding:40px; text-align:center; color:#999;">✓ 未发现特定金额交易</div>';
    }

    let html = `
      <table class="analysis-result-table" style="width:100%; border-collapse:collapse; font-size:0.9rem;">
        <thead style="background:#f5f7fa;">
          <tr>
            <th style="padding:10px; border:1px solid #e0e0e0;">账号/户名</th>
            <th style="padding:10px; border:1px solid #e0e0e0;">交易时间</th>
            <th style="padding:10px; border:1px solid #e0e0e0; text-align:right; background:#fff3cd;">交易金额</th>
            <th style="padding:10px; border:1px solid #e0e0e0;">对方</th>
          </tr>
        </thead>
        <tbody>
    `;

    data.forEach((row, index) => {
      const rowStyle = index % 2 === 0 ? 'background:#fafafa;' : '';
      const amountColor = row.debit_credit_flag === '借' ? '#67c23a' : '#f56c6c';
      html += `<tr style="${rowStyle}">`;
      html += `<td style="padding:8px; border:1px solid #e0e0e0;">${row.customer_name}<br><small style="color:#909399;">${row.account_card}</small></td>`;
      html += `<td style="padding:8px; border:1px solid #e0e0e0;">${this.formatDate(row.transaction_time)}</td>`;
      html += `<td style="padding:8px; border:1px solid #e0e0e0; text-align:right; color:${amountColor}; font-weight:bold;">¥${this.formatAmount(row.transaction_amount)}</td>`;
      html += `<td style="padding:8px; border:1px solid #e0e0e0;">${row.counterparty_name || '-'}</td>`;
      html += '</tr>';
    });

    html += '</tbody></table>';
    return html;
  }
}

window.SpecialAmountAnalyzer = SpecialAmountAnalyzer;
